<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>

    <tab-control
      class="tab-control"
      :titles="['流行', '新款', '推荐']"
      @tabClick="tabClick"
      ref="tabControl1"
      v-show="isTabFixed"
    ></tab-control>

    <scroll
      class="content"
      ref="scroll"
      :probe-type="2"
      :pull-up-load="true"
      @scroll="contentScroll"
      @pullUpload="loadMore"
    >
      <home-swiper
        :banners="banners"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control
        class="tab-control"
        :titles="['流行', '新款', '推荐']"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShow"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/scroll";

import { getHomeMultidata, getHomeGoods } from "network/home";
import {itemListenerMixin,backTopMixin} from "common/mixin" //防抖函数和mixin使用的封装
export default {
  name: "Home",
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      current: "pop",
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY:0
    };
  },
/* 解决better-scroll 吸顶bug 利用 activated和deactivated */
  activated(){
    /* 当组件激活时 y轴滚动到saveY的位置 */
    this.$refs.scroll.refresh()
    this.$refs.scroll.scrollTo(0,this.saveY)
    
  },
  deactivated(){
    /* 当组件停用时 记录滚动的y轴的值 */
    this.saveY = this.$refs.scroll.scroll.y
  },
  created() {
    this.getHomeMultidata();

    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  //防抖函数和mixin使用的封装
  mixins:[itemListenerMixin,backTopMixin],
  computed: {
    showGoods() {
      return this.goods[this.current].list;
    },
  },
  methods: {
    tabClick(index) {
      //点击谁current就记录谁
      switch (index) {
        case 0:
          this.current = "pop";
          break;
        case 1:
          this.current = "new";
          break;
        case 2:
          this.current = "sell";
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullUp();
      });
    },

    /* 调用scroll.vue 中的scrollTo方法实现返回顶部方法 */

    contentScroll(position) {
      //判断backtop是否显示
      this.listenBackTop(position)
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },

    loadMore() {
      this.$refs.scroll.scroll.refresh();
      this.getHomeGoods(this.current);
    },
    swiperImageLoad() {
      /* 获取TabControl的offsettop */
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
  },
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
  },
};
</script>
<style scoped>
#home {
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: white;
  position: fixed;
  z-index: 100;
  width: 100%;
  top: 0;
  left: 0;
  right: 0;
}
.tab-control {
  position: sticky;
  top: 44px;
}
.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>
